﻿Public Class MailSettingsControl
    Private Sub MailSettingsControl_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If My.Settings.MailServerSettings IsNot Nothing Then
            txtFromEmail.Text = My.Settings.MailServerSettings.FromEmail
            txtFromName.Text = My.Settings.MailServerSettings.FromName
            txtSmtpHost.Text = My.Settings.MailServerSettings.SmtpHost
            txtSmtpPort.Text = My.Settings.MailServerSettings.SmtpPort

            If My.Settings.MailServerSettings.SmtpAuthentication Then
                rbSmtpAuthenticationYes.Select()
            Else
                rbSmtpAuthenticationNo.Select()
            End If

            txtSmtpUsername.Text = My.Settings.MailServerSettings.SmtpUsername
            txtSmtpPassword.Text = My.Settings.MailServerSettings.SmtpPassword.ToString

            If My.Settings.MailServerSettings.SmtpSsl Then
                rbSmtpSecuritySSL.Select()
            Else
                rbSmtpSecurityNone.Select()
            End If
        End If
    End Sub

    Private Sub rbSmtpAuthenticationYes_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbSmtpAuthenticationYes.CheckedChanged
        If rbSmtpAuthenticationYes.Checked Then
            txtSmtpUsername.Enabled = True
            txtSmtpPassword.Enabled = True
        End If
    End Sub

    Private Sub rbSmtpAuthenticationNo_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbSmtpAuthenticationNo.CheckedChanged
        If rbSmtpAuthenticationNo.Checked Then
            txtSmtpUsername.Enabled = False
            txtSmtpPassword.Enabled = False
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If My.Settings.MailServerSettings Is Nothing Then
            My.Settings.MailServerSettings = New MailServerSettings
        End If

        My.Settings.MailServerSettings.FromEmail = txtFromEmail.Text
        My.Settings.MailServerSettings.FromName = txtFromName.Text
        My.Settings.MailServerSettings.SmtpHost = txtSmtpHost.Text
        My.Settings.MailServerSettings.SmtpPort = Convert.ToInt16(txtSmtpPort.Text)
        My.Settings.MailServerSettings.SmtpAuthentication = If(rbSmtpAuthenticationYes.Checked, True, False)
        My.Settings.MailServerSettings.SmtpUsername = txtSmtpUsername.Text
        My.Settings.MailServerSettings.SmtpPassword = txtSmtpPassword.Text
        My.Settings.MailServerSettings.SmtpSsl = If(rbSmtpSecuritySSL.Checked, True, False)

        MessageBox.Show("Changes saved!", "MailSettings", MessageBoxButtons.OK)
    End Sub
End Class
